package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.medical.dao.local.PrintTemplateMapper;
import com.zhijian.medical.entity.po.PrintTemplate;
import com.zhijian.medical.entity.vo.request.PrintTemplateSearchVo;
import com.zhijian.medical.entity.vo.response.PrintTemplateVo;
import com.zhijian.medical.service.PrintTemplateService;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 打印模板业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2023-09-22 10:03:34
 */
@Slf4j
@Service
public class PrintTemplateServiceImpl implements PrintTemplateService {

    @Autowired
    private PrintTemplateMapper mapper;

    @Override
    public PrintTemplate selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public PrintTemplateVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(PrintTemplate printTemplate) {
        Date now = new Date();
        printTemplate.setCreateTime(now);
        printTemplate.setUpdateTime(now);
        return mapper.insert(printTemplate) > 0 ? printTemplate.getId() : 0L;
    }

    @Override
    public boolean update(PrintTemplate printTemplate) {
        Date now = new Date();
        printTemplate.setUpdateTime(now);
        return mapper.updateByPrimaryKey(printTemplate) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(PrintTemplate record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public Integer count(PrintTemplateSearchVo printTemplateSearchVo) {
        return mapper.count(printTemplateSearchVo);
    }

    @Override
    public List<PrintTemplateVo> selectList(PrintTemplateSearchVo printTemplateSearchVo) {
        return mapper.selectList(printTemplateSearchVo);
    }

    @Override
    public Page<PrintTemplateVo> selectPage(PrintTemplateSearchVo printTemplateSearchVo) {
        //先直接查总数
        int count = count(printTemplateSearchVo);
        //分页
        Page<PrintTemplateVo> page = new Page<>(true, count, printTemplateSearchVo.getPageNum(), printTemplateSearchVo.getPageSize());
        printTemplateSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<PrintTemplateVo> responseVos = this.selectList(printTemplateSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<PrintTemplate> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

}